﻿using loginWinForm.common;
using loginWinForm.content;
using loginWinForm.entity;
using loginWinForm.service;
using loginWinForm.service.impl;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace loginWinForm.form
{
    public partial class Success : Form
    {
        ICodingService codingService = new CodingService();
        public Success()
        {
            if (null == CONTENT.SysUser)
            {
                ResultBox.fail("请登录后访问","登录提示");
                this.Hide();
                new login().Show();
            }
            else
            {
                InitializeComponent();
                this.successText.Text = "欢迎您 - " + CONTENT.SysUser.Mc;
            }
            
        }
        
        // 退出登录事件
        private void logout_Click(object sender, EventArgs e)
        {
            this.Hide();
            CONTENT.SysUser = null;
            new login().Show();
        }

        // 修改密码事件
        private void button1_Click(object sender, EventArgs e)
        {
            this.Hide();
            new ChangePwd().Show();
        }

        // 窗口加载事件
        private void Success_Shown(object sender, EventArgs e)
        {
            Dictionary<string, string> map = new Dictionary<string, string>();
            this.getDataList(map);
        }

        // 重置事件
        private void resetBtn_Click(object sender, EventArgs e)
        {
            bookName.Text = "";
            codeName.Text = "";
            Dictionary<string, string> map = new Dictionary<string, string>();
            this.getDataList(map);
        }
        
        // 查询事件
        private void searchBtm_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> map = new Dictionary<string, string>();
            string book = bookName.Text;
            string coding = codeName.Text;
            if (book.Length > 0)
            {
                map.Add("book", book);
            }
            if (coding.Length > 0)
            {
                map.Add("coding", coding);
            }
            this.getDataList(map);
        }

        private void getDataList(Dictionary<string, string> map)
        {
            this.dataGridView1.Rows.Clear();
            List<CodeInfo> codes = codingService.GetCodes(map);
            codes.ForEach(code =>
            {
                initdataGrid(code);
            });
        }
        private void initdataGrid(CodeInfo code)
        {
            int index = this.dataGridView1.Rows.Add();
            // 序号
            this.dataGridView1.Rows[index].Cells[0].Value = index + 1 + "";
            // 编程语言
            this.dataGridView1.Rows[index].Cells[1].Value = code.Coding;
            // 推荐编译器
            this.dataGridView1.Rows[index].Cells[2].Value = code.Ide;
            // 推荐书籍
            this.dataGridView1.Rows[index].Cells[3].Value = code.Book;
            // 操作人
            this.dataGridView1.Rows[index].Cells[4].Value = code.UpdateName;
            // 操作时间
            this.dataGridView1.Rows[index].Cells[5].Value = code.UpdateTime;
        }

        // 新增数据
        private void addCode_Click(object sender, EventArgs e)
        {
            ResultBox.warning("新增数据-待实现");
        }

        // 修改数据
        private void updateCode_Click(object sender, EventArgs e)
        {
            ResultBox.warning("修改数据-待实现");
        }

        // 删除方法
        private void deleteCode_Click(object sender, EventArgs e)
        {
            ResultBox.warning("删除数据-待实现");
        }
    }
}
